package com.netflexitysolutions.amazonws.ec2.internal.operations;

import com.amazonaws.ec2.doc._2009_04_04.AuthorizeSecurityGroupIngressType;
import com.amazonaws.ec2.doc._2009_04_04.IpPermissionSetType;
import com.netflexitysolutions.amazonws.ec2.EC2;
import com.netflexitysolutions.amazonws.ec2.IPPermission;
import com.netflexitysolutions.amazonws.ec2.internal.ModelUtil;

public class AuthorizeSecurityGroupIngressOperation extends SingleObjectOperationExecutor<AuthorizeSecurityGroupIngressType, Boolean> {
	
	private IPPermission permission;
	
	private String userId;
	
	public AuthorizeSecurityGroupIngressOperation(EC2 ec2, String userId, String groupName, IPPermission permission) {
		super(ec2, groupName);
		this.permission = permission;
		this.userId = userId;
	}

	@Override
	protected Boolean call(AuthorizeSecurityGroupIngressType request) {
		request.setGroupName(objectId);
		request.setUserId(userId);
		IpPermissionSetType set = new IpPermissionSetType();
		request.setIpPermissions(set);
		set.getItem().add(ModelUtil.toXMLType(permission));
		return getEC2().getService().authorizeSecurityGroupIngress(request).isReturn();
	}

}
